package com.wanwu.channel.facade.dto.resp;

import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

@Data
public class GatewayResult<T> implements Serializable {

    private static final long serialVersionUID = 2357458648498664726L;

    private static final int SUCCESS_CODE = 1;

    private static final int FAIL_CODE = 0;

    private int code;
    private String msg;
    private T data;

    public static <T> GatewayResult<T> wrapSuccess(T data) {
        GatewayResult<T> result = new GatewayResult<T>();
        result.setCode(SUCCESS_CODE);
        result.setMsg("");
        result.setData(data);
        return result;
    }

    public static <T> GatewayResult<T> wrapFail(T data, String errorMsg) {
        GatewayResult<T> result = new GatewayResult<T>();
        result.setCode(FAIL_CODE);
        result.setMsg(errorMsg);
        result.setData(data);
        return result;
    }

    public static <T> GatewayResult<T> wrapFail(String errorMsg) {
        GatewayResult<T> result = new GatewayResult<T>();
        result.setCode(FAIL_CODE);
        result.setMsg(errorMsg);
        return result;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}
